package com.right.health.app.ui.registered;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.magic.basic.adapter.BaseRecyclerAdapter;
import com.magic.basic.utils.AppUtil;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.bean.DepartmentInfo;
import com.right.health.app.bean.DepartmentResponse;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.DeptApi;
import com.right.health.app.ui.registered.adapter.DepartmentLeftAdapter;
import com.right.health.app.ui.registered.adapter.DepartmentRightAdapter;

import java.util.ArrayList;
import java.util.List;

/*
 * 选择科室
 * Created by hopeliao on 2019-09-24
 *
 */
public class ChooseDepartmentActivity extends BasePresenterActivity implements OnHttpListener {

    public static final String EXTRA_CODE = "extra:code";

    private DepartmentLeftAdapter mLeftAdapter;

    private ListView mLeftListView;

    private DepartmentRightAdapter mRightAdapter;

    private DeptApi mDeptApi;

    public List<DepartmentInfo> mDataSource = new ArrayList<>();

    private String mCode;

    private EditText mSearchEt;

    private String mSearchName;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.choose_department);
    }

    @Override
    public void findView() {
        mSearchEt = (EditText) findViewById(R.id.et_search);
        mSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mSearchName = s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mSearchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    AppUtil.hideSoftInput(mSearchEt);
                    doRefresh();
                    return true;
                }
                return false;
            }
        });

        mLeftListView = (ListView) findViewById(R.id.left_list);

        mLeftAdapter = new DepartmentLeftAdapter(this);
        mLeftListView.setAdapter(mLeftAdapter);

        mRightAdapter = new DepartmentRightAdapter(this);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.right_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mRightAdapter);
        mRightAdapter.setOnItemClickListener(new BaseRecyclerAdapter.ItemClickListener() {
            @Override
            public void onItemClickListener(int i) {
                DepartmentInfo info = mDataSource.get(i);
                Bundle bundle = new Bundle();
                bundle.putParcelable(BroadCastKey.DATA, info);
                bundle.putString(EXTRA_CODE, mCode);
                startIntent(ChooseDoctorActivity.class,bundle);
            }
        });

//        recyclerView.addItemDecoration(
//                new HorizontalDividerItemDecoration.Builder(this)
//                        .color(getResources().getColor(R.color.color_EDEDED))
//                        .sizeResId(R.dimen.dp_0_5)
//                        .build());
    }


    @Override
    public void initialize() {
        mDeptApi = new DeptApi();
        mDeptApi.setListener(this);

        Bundle bundle = getIntent().getExtras();
        if(bundle != null && bundle.containsKey(EXTRA_CODE)) {
            mCode = bundle.getString(EXTRA_CODE);
        }

        doRefresh();
    }

    private void doRefresh() {
        if(TextUtils.isEmpty(mCode)) {
            mDeptApi.getDepartmentList(getResources().getString(R.string.def_hospital_code), mSearchName);
        } else {
            mDeptApi.getDepartmentList(mCode, mSearchName);
        }
    }

    public static void applyActivity(Activity act, String code) {
        Intent intent = new Intent(act, ChooseDepartmentActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_CODE, code);
        intent.putExtras(bundle);
        act.startActivity(intent);
    }

    @Override
    public void onResponse(Request request, Response response) {
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if(response.targetData instanceof DepartmentResponse) {
                DepartmentResponse departmentResponse = response.cast(DepartmentResponse.class);
                mDataSource.clear();
                mDataSource.addAll(departmentResponse.hospitalDepts);

                mRightAdapter.setDataSource(mDataSource);
            }
        }
    }
}
